package mblog.web.controller;

import mblog.base.utils.CheckSumBuilder;
import mblog.base.utils.HttpUtils;
import mblog.core.persist.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;

@RestController
@RequestMapping(value="/redis")
public class redisTestController {
    @Autowired
    private RedisService redisService;

    @RequestMapping(value = "/test", method = {RequestMethod.GET, RequestMethod.POST})
    public String redisTest(){


        String msgUrl = "https://api.netease.im/sms/sendcode.action";
        HashMap<String,String> dataMap = new HashMap<>();
        String appKey = "008d8c7d18f6509087607876fe24538a";
        dataMap.put("AppKey",appKey);
        String times = String.valueOf(System.currentTimeMillis());
        dataMap.put("Nonce",times);
        dataMap.put("CurTime",times);
        String checkSum = CheckSumBuilder.getCheckSum(appKey,times,times);
        dataMap.put("CheckSum",checkSum);
        dataMap.put("mobile","15618785577");
        try {
            HttpUtils.post(msgUrl,dataMap);
        } catch (IOException e) {
            e.printStackTrace();
        }


        StringBuffer sb = new StringBuffer();
        redisService.set("str", "str");
        sb.append("str=").append(redisService.get("str").toString()).append(",");
        redisService.hmSet("hmset","key","val");
        sb.append("hmset=").append(redisService.hmGet("hmset","key")).append(",");
        redisService.lPush("list","val");
        sb.append("list=").append(redisService.lRange("list",0,1).toString()).append(",");
        redisService.add("set","val");
        sb.append("set=").append(redisService.setMembers("set").toString()).append(",");
        redisService.zAdd("zset","val1",1);
        redisService.zAdd("zset","val2",2);
        sb.append("zset=").append(redisService.rangeByScore("zset",1,2)).append(",");
        return sb.toString();
    }
}